package cn.bdqn.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.bean.User;
import cn.bdqn.service.UserService;
import cn.bdqn.service.impl.UserServiceImpl;
import cn.bdqn.util.Constant;

public class CheckUserServlet extends HttpServlet {

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//获取ajax方法传过来的数据
		String userCode = request.getParameter("userCode");
		User user = new User();
		UserService userService = new UserServiceImpl();
		String msg="";
		PrintWriter out = response.getWriter();
		if(userCode!=null && userCode!=""){
			//给user赋值
			user.setUserCode(userCode);
			User u = userService.login(user);
			if(u==null || u.getUserCode()==null){
				//没有找到相同的userCode
				msg=Constant.SUCCESS;
			}else{
				//有相同的userCode，提示用户已存在
				//不能使用
				msg=Constant.EXIST;
			}
			out.print(msg);
			out.close();
		}
	}

}
